package com.situ.springboot.controller;

import com.situ.springboot.pojo.Blog;
import com.situ.springboot.pojo.query.BlogQuery;
import com.situ.springboot.service.IBlogService;
import com.situ.springboot.util.PageResult;
import com.situ.springboot.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/blog")
public class BlogController {
    //注入对象使用
    //@Autowired
    //private BlogMapper blogMapper;
    @Autowired
    private IBlogService blogService;

    // /blog/list
    @RequestMapping("/list1")
    @ResponseBody   //以json形式返回数据
    public List<Blog> list1() {
        System.out.println("BlogController.list1");
        List<Blog> list = blogService.list1();
        return list;
    }

    // /blog/list?page=1&limit=10&name=zhangsan&email=233@qq.com
    @RequestMapping("/list")
    @ResponseBody   //以json形式返回数据
    public PageResult list(BlogQuery blogQuery) {
        System.out.println("BlogController.list");
        PageResult pageResult = blogService.list(blogQuery);
        return pageResult;
    }

    //转发页面，返回String，不需要加@ResponseBody
    @RequestMapping("/toBlogList")
    public String toBlogList() {
        return "blog_list";
    }

    @RequestMapping("/deleteById")
    @ResponseBody
    public Result deleteById(Integer id) {
        blogService.deleteById(id);
        return Result.ok("删除成功");
    }

    @RequestMapping("/deleteAll")
    @ResponseBody
    public Result deleteAll(Integer[] ids) {
        //Alt+Enter
        blogService.deleteAll(ids);
        return Result.ok("删除成功");
    }

    @RequestMapping("/toBlogAdd")
    public String toBlogAdd() {
        return "blog_add";
    }

    @RequestMapping("/add")
    @ResponseBody
    public Result add(Blog blog) {
        blogService.add(blog);
        return Result.ok("添加成功");
    }

    @RequestMapping("/toBlogUpdate")
    public String toBlogUpdate() {
        return "blog_update";
    }

    @RequestMapping("/selectById")
    @ResponseBody
    public Result selectById(Integer id) {
        Blog blog = blogService.selectById(id);
        return Result.ok(blog);
    }

    @RequestMapping("/update")
    @ResponseBody
    public Result update(Blog blog) {
        blogService.update(blog);
        return Result.ok("修改成功");
    }

}
